﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    { 
        [ChildActionOnly]
        public ActionResult SLCKGBN(string returnUrl)
        {
            //插入到一个数据库表，然后批量进行移动记录，记录下来时间戳和申请的ID号码

            ModelState.Clear();
            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "CW";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "KO";
            MABF.CWBB.LP = "        "; 
            return PartialView(MABF.CWBB);
        }
       
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SLCKGBN(WBBSTModel model, string returnUrl)
        {
            WZ09GBN(model);//发货操做 
            ModelState.Clear();
            return RedirectToAction(MAPP, MAPP);
        } 
        public void WZ09GBN(WBBSTModel model)
        {
            try
            {
                if (model == null)
                    return;
                INFO = "不存在";
                dwData = VHP.VWBB.TBMain.NewRow();
                dwData["WBBMANDANT"] = app.Mandant;
                dwData["WBBGEBNR"] = model.GEBNR.PadRight(8);
                dwData["WBBAUFLFD"] = " AND WBBAUFLFD='000000'";
                SResult = VHP.VWBB.QWBGEBNR(dwData);
                
                foreach (DataRow _dwWB in SResult.Tables[0].Rows)
                {
                    string art = _dwWB["WBBART"].ToString() + _dwWB["WBBFARBE"].ToString();
                    if(MABF.CVVT.VFART!=art)
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "不在范围内;";
                        continue;
                    }
                    if (MABF.CVVT.LIEFSCH == _dwWB["WBBURLIEFSCH"].ToString())
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "已经发货;";
                        continue;
                    }
                    if (MABF.CVVT.VKPAUF == _dwWB["WBBAUFKRS"].ToString()+_dwWB["WBBAUFLFD"].ToString() + _dwWB["WBBAUFPOS"].ToString())
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "已经发货;";
                        continue;
                    }
                    _dwWB["WBBAUFTYP"] = "K";
                    _dwWB["WBBAUFKRS"] = MABF.CVVT.KRS;
                    _dwWB["WBBAUFLFD"] = MABF.CVVT.LFD;
                    _dwWB["WBBAUFPOS"] = MABF.CVVT.POS;
                    _dwWB["WBBFERTKZ"] = "E";
                    _dwWB["WBBURLIEFSCH"] = MABF.CVVT.LIEFSCH;
                    _dwWB["WBBALIEFSCH"] = MABF.CVVT.LIEFSCH;
                    _dwWB["WBBAENDBEN"] = MABF.NEWUSER;
                    _dwWB["WBBAENDDAT"] = VHP.VWBB.DBDATE;
                    _dwWB["WBBGEBNR"] = "".PadRight(8);
                    VHP.VWBB.UWB09(_dwWB);
                    VHP.VWBW.NWBBEW(_dwWB, "61", RUNID);//增加留位移动记录 
                    HWBS++;
                }
                VHP.VWBW.POSTCHG();
                PM["AUFTYP"] = "K";
                PM["KRS"] = MABF.CVVT.KRS;
                PM["LFD"] = MABF.CVVT.LFD;
                PM["POS"] = MABF.CVVT.POS;
                PM["VKPAUF"] = MABF.CVVT.KRS + MABF.CVVT.LFD + MABF.CVVT.POS;
                PM["ORDERNUM"] = "K" + MABF.CVVT.KRS + MABF.CVVT.LFD + MABF.CVVT.POS;
                PM["LIEFSCH"] = MABF.CVVT.LIEFSCH;
                VHP.VVT.VVTCHG(PM);
                VHP.VVVL.VVCHG(PM);
            }
            catch(Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            
        }
    }
}
